package com.leancloud.login;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

public class TimingTextView extends android.support.v7.widget.AppCompatTextView {
    private final int MSG_WHAT_START = 10_010;

    private String startString;
    private String endString;
    private long overallTime;  //总时间
    private long timeDifference;//时间差
    private long mLastMillis;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_WHAT_START:
                    if (mLastMillis > 0) {
                        setAvailable(false);
                        mLastMillis -= timeDifference;
                        mHandler.sendEmptyMessageDelayed(MSG_WHAT_START, timeDifference);
                    } else {
                        mHandler.removeMessages(MSG_WHAT_START);
                        setAvailable(true);
                    }
                    break;
            }
        }
    };

    public TimingTextView(Context context) {
        super(context);
        initAttrs(context, null, 0);
    }

    public TimingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs, 0);
    }

    public TimingTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs, defStyleAttr);
    }

    private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimingTextView, defStyleAttr, 0);
        startString = a.getString(R.styleable.TimingTextView_startHintText);
        endString = a.getString(R.styleable.TimingTextView_endHintText);
        overallTime = a.getInteger(R.styleable.TimingTextView_overallTime, 60000);
        timeDifference = a.getInteger(R.styleable.TimingTextView_timeDifference, 1000);

        setClickable(true);
        setFocusable(true);
        setDuplicateParentStateEnabled(true);

        createData();
    }

    private void createData() {
        setText(startString);
        mLastMillis = overallTime;
        mHandler.removeMessages(MSG_WHAT_START);
    }

    public void reset() {
        mLastMillis = overallTime;
        setEnabled(true);
        mHandler.removeMessages(MSG_WHAT_START);
        mHandler.sendEmptyMessage(MSG_WHAT_START);
    }

    private void setAvailable(boolean available) {
        if (available) {
            setEnabled(true);
            setText(endString);
        } else {
            setEnabled(false);
            setText(mLastMillis / 1000 + "s");
        }
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        super.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                reset();
                onClickListener.onClick(v);
            }
        });
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeMessages(MSG_WHAT_START);
    }
}
